$.Class("Transaction.Grid",
// static properties
{     
    addParams : function(e, params)
    {
        e.sender.transport.options.read.data = params;
    }    
},
// prototype properties
    {
        // constructor function
        init: function(id) {
            this.id = id;
            
            this.dataSource = this.createDataSource();
            this.createGrid();     
            this.initEvents(id);
        },
        createDataSource: function()
        {
            var dataSource = new kendo.data.DataSource({
                transport: {
                    read: {
                        url: "/invest/getTransaction",
                        dataType: "json",
                        type: "post",
                        data : {}
                    }
                },
                serverPaging: false,
                schema: {
                    data: "results",
                    total: function(data) {
                        return data.total;
                    }
                },
                requestStart: function(e) {
                    var data = {
                        id : $("#investId").val(),
                        isAjax : true
                    };
                    
                    Transaction.Grid.addParams(e, data);
                }
            });

            return dataSource;
        },
        createGrid : function()
        {
            $("#" + this.id).kendoGrid({
	        dataSource: this.dataSource,
	        columns: [
            {
                field:"grid_index",
                title:" ",
                width: 30
            },{
                field:"withdraw",
                title:"Withdraw",
                width: 100,
                template: '$${withdraw}'
            },{
                field:"fee",
                title:"Fee",
                width: 70,
                template: '$${fee}'
            },{
                field:"received",
                title:"Received",
                width: 100,
                template: '$${received}'
            },{
                field:"isRCB",
                title:"RCB",
                width: 55,
                template: '${isRCB}'
            },{
                field:"createdDate",
                title:"Date",
                width: 160
            },{
            	title:"",
                field:"",         
                width: 65,
                template: '<a href="" onclick="return Transaction.delete(${id});">Delete</a>'
            },{
            	title:"",
                field:"",                
            }],
            editable:false,
            selectable: 'row',
            dataBound: function(e) {                
                
            }
        });	
        },
        initEvents : function(id)
        {
           
        },
        refresh : function()
        {
            $("#" + this.id).data("kendoGrid").dataSource.read();	// Reset grid
        }
})

Transaction.create = function()
{
    var isRCB = 0;
    if($("#isRCB").attr("checked") == "checked"){
        isRCB = 1;
    }

    $.ajax({
        type: 'post',
        url: '/invest/createTransaction',
        data: {
            investId : $("#investId").val(),
            withdraw : $("#withdraw").val(),
            fee : $("#fee").val(),
            isRCB : isRCB,
            transactionDate : $("#transactionDate").val()
        },
        dataType: 'json',
        success: function(obj){
            $("#withdraw").data('kendoNumericTextBox').value(0);
            $("#gridTransaction").data("kendoGrid").dataSource.read();
        },
    });
}

Transaction.delete = function(id)
{
    if(confirm("Do you want to delete?"))
    {
        $.ajax({
            type: 'post',
            url: '/invest/deleteTransaction',
            data: {
                id : id
            },
            dataType: 'json',
            success: function(obj){
                $("#gridTransaction").data("kendoGrid").dataSource.read();
            },
        });
    }
    
    return false;
}